package top.sdwu_top.kbase.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.sdwu_top.kbase.common.annocation.CurrentUser;
import top.sdwu_top.kbase.common.model.Wrapper;
import top.sdwu_top.kbase.dto.KbaseKnowledgeTagDto;
import top.sdwu_top.kbase.entity.KbaseAuthUser;
import top.sdwu_top.kbase.entity.KbaseKnowledgeTag;
import top.sdwu_top.kbase.service.IKbaseKnowledgeTagService;
import top.sdwu_top.kbase.vo.KbaseKnowledgeTagVo;

import java.util.List;

/**
 * <p>
 * 知识标签 前端控制器
 * </p>
 *
 * @author zhipeng.duan@nvr-china.com
 * @since 2020-02-20
 */
@RestController
@RequestMapping("api/knowledge-tag")
public class KbaseKnowledgeTagController {

    @Autowired
    IKbaseKnowledgeTagService knowledgeTagService;

    @GetMapping("{id}")
    public Wrapper<KbaseKnowledgeTagDto> getChain(@PathVariable("id") String id) {
        return Wrapper.ok(knowledgeTagService.getChain(id));
    }

    @PostMapping("page")
    public Wrapper<Page<KbaseKnowledgeTag>> page(@RequestBody KbaseKnowledgeTagVo knowledgeTagVo) {
        return Wrapper.ok(knowledgeTagService.page(knowledgeTagVo));
    }

    @PostMapping
    public Wrapper<String> create(@RequestBody KbaseKnowledgeTag knowledgeTag) {
        if (knowledgeTagService.save(knowledgeTag)) {
            return Wrapper.ok(knowledgeTag.getTagName() + "新建成功");
        }
        return Wrapper.error(knowledgeTag.getTagName() + "新建失败");
    }

    @PutMapping
    public Wrapper<String> update(@CurrentUser KbaseAuthUser user ,@RequestBody KbaseKnowledgeTag knowledgeTag) {
        if (knowledgeTagService.update(user, knowledgeTag)) {
            return Wrapper.ok("修改成功");
        }
        return Wrapper.error("修改失败");
    }

    @DeleteMapping("{id}")
    public Wrapper<String> delete(@CurrentUser KbaseAuthUser user ,@PathVariable("id") String id) {
        if (knowledgeTagService.delete(user, id)) {
            return Wrapper.ok("删除成功");
        }
        return Wrapper.error("删除失败");
    }

    @GetMapping("tree")
    public Wrapper<List<KbaseKnowledgeTag>> getTagTree(@CurrentUser KbaseAuthUser user) {
        return Wrapper.ok(knowledgeTagService.getTagTreeList(user));
    }

}

